/**
 * 
 */
package com.fox.webapp.web.admin;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fox.webapp.domain.Account;
import com.fox.webapp.service.IAccountService;
import com.fox.webapp.utils.DataTablesRequest;
import com.fox.webapp.utils.DataTablesResponse;

/**
 * @author Yockii
 *
 */
@Controller
@RequestMapping("admin/commission")
public class CommissionAdminController {
	
	@Autowired
	private IAccountService accountService;

	@RequestMapping(value = {"import"}, method = RequestMethod.GET)
	public String commissionPage(){
		return "admin/commission/commission";
	}
	
	@RequestMapping(value = "/doCommissionRecord", method = {RequestMethod.POST})
	public String executeExcel(Model model, @RequestParam("name") String name, 
        @RequestParam("file") MultipartFile file){
		try{
			if(!file.isEmpty()){
				String result = accountService.recordCommissionWithExcel(file.getInputStream());
				model.addAttribute("msg", result);
			}
		} catch (Exception e) {	
			model.addAttribute("msg", e.getLocalizedMessage());
		}
		return commissionPage();
	}
	
	@RequestMapping(value = "needPayCommission", method = RequestMethod.GET)
	public String needPayCommission(){
		return "admin/commission/needPay";
	}
	
	@RequestMapping(value = "needPayAccount", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesResponse<Account> getAllAccountNeedPay(Account account, DataTablesRequest req, DataTablesResponse<Account> res){
		account.setLookingForPay(true);
		Page<Account> ap = accountService.getAccountWithCondition(account, req.getiDisplayStart()/req.getiDisplayLength(), req.getiDisplayLength());
		res.setAaData(ap.getContent());
		res.setiTotalDisplayRecords(ap.getNumberOfElements());
		res.setiTotalRecords(ap.getNumberOfElements());
		return res;
	}
	
	@RequestMapping(value = "refundDone", method = RequestMethod.POST)
	@ResponseBody
	public int accountRefundDone(Account account, Double commissionRefund){
		return accountService.commissionRefundDone(account, commissionRefund);
	}
}
